{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f314fc67-f8c5-4435-9ed7-c49316aaa01c",
   "metadata": {},
   "source": [
    "## enumerate函数\n",
    "enumerate() 函数用于将一个可遍历的数据对象(如**列表、元组或字符串**)组合为一个索引序列，同时列出数据和数据下标，一般用在 for 循环当中。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5cc719f9-37ec-41eb-bedb-34c8295d1e7d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<enumerate object at 0x000001E058636F70>\n",
      "[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]\n"
     ]
    }
   ],
   "source": [
    "seasons = ['Spring', 'Summer', 'Fall', 'Winter']\n",
    "print(enumerate(seasons)) #打印一个地址\n",
    "print(list(enumerate(seasons)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2f5c558-3a58-4285-8955-d42e1ae3226c",
   "metadata": {},
   "source": [
    "## 比较常见的作法是 反转键值对\n",
    "比方说,对于上面的例子,键是0,1,2,3;值是Spring,Summer,Fall,Winter."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f7c71e44-9060-4e85-8707-9b5aec8c661c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0: 'Spring', 1: 'Summer', 2: 'Fall', 3: 'Winter'}\n",
      "{0: 'Spring', 1: 'Summer', 2: 'Fall', 3: 'Winter'}\n"
     ]
    }
   ],
   "source": [
    "print(dict(enumerate(seasons)))\n",
    "print( {k:v for k,v in enumerate(seasons) } )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8f4b9de8-b92a-4c38-a4e6-443d1a5d0702",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'I': 0, 'will': 1, 'miss': 2, 'your': 3, 'bright': 4, 'eyes': 5, 'and': 6, 'sweet': 7, 'smile': 8}\n"
     ]
    }
   ],
   "source": [
    "texts = \"I will miss your bright eyes and sweet smile\"\n",
    "word_list = texts.split(\" \")\n",
    "tokens = {v: k for k,v in enumerate(word_list)}\n",
    "print(tokens)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ce5dd044-c6a8-48c6-80e4-4cc16a3cd915",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.3.1+cpu\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "print(torch.__version__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6e8a7df9-8268-4f59-8c39-e4ad1d24fdfb",
   "metadata": {},
   "outputs": [],
   "source": [
    "torch.cuda.is_available()\n",
    "N = 1000\n",
    "device = 'cuda' if torch.cuda.is_available() is True else 'cpu'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "881de181-09d7-4b16-ac24-1c1c9ab6d4c5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8.02 ms ± 254 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
     ]
    }
   ],
   "source": [
    "x = torch.randn(N,N).to(device = device)\n",
    "%timeit (x @ x).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3a5c0338-840c-4db1-a339-5e8c6ca43386",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始x的地址2080183462288\n",
      "赋值后x的地址2080183452880\n",
      "使用原地修改后，x的地址2080183452880\n"
     ]
    }
   ],
   "source": [
    "# 原地修改\n",
    "x = torch.randn(3,4)\n",
    "y = torch.randn(3,4)\n",
    "print(f'原始x的地址{id(x)}')\n",
    "x = x + y\n",
    "print(f'赋值后x的地址{id(x)}')\n",
    "x[:] = x + y\n",
    "print(f'使用原地修改后,x的地址{id(x)}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "0d8c7eda",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-2.8304404e-01  2.1363978e+00 -2.6605096e+00 -1.9408931e-01]\n",
      " [ 5.6972986e-01  1.0000000e+03 -3.4814352e-01 -1.7523054e+00]\n",
      " [-1.7767067e+00  2.7286047e-01  2.2667009e-02  3.1630401e-02]]\n"
     ]
    }
   ],
   "source": [
    "# tensor 与 ndarray的区别\n",
    "x = torch.randn(3,4)\n",
    "a = x.numpy() # a与x共享内存数据\n",
    "b = torch.tensor(a)\n",
    "type(a),type(b)\n",
    "\n",
    "x[1,1] = 1000 # a[1,1]也会发生修改\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "fde0c02e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2080183057680, 2080189821840)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "id(x),id(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "accc5aca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([ -1.0012, 998.4692,  -1.4495]),\n",
       " torch.return_types.max(\n",
       " values=tensor([2.1364e+00, 1.0000e+03, 2.7286e-01]),\n",
       " indices=tensor([1, 1, 1])))"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 降维\n",
    "x.sum(dim = 1),x.max(dim=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "bf6282cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([4, 5])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = torch.randn(4,5,6)\n",
    "y.max(dim = 2)[0].shape #形状应该是(4,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81997cd9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
